﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
using ProtoBuf.Message;
using System.Collections.Generic;
using Chat;
namespace UI
{
    public class UIUnionFightChatPage : MonoBehaviour
    {
        public GameObject myItem;
        public GameObject otherItem;
        public UIGrid grid;
        public UIScrollView scrollView;
        public UIInput input;
        public UILabel textNums;
        private string content;
        private List<GameObject> items = new List<GameObject>();
        private ChatInfo lastShawInfo = null;
        private int maxNum;

        void Start() 
        {
            grid.onReposition = OnGridReposition;
            GameObjectPoolManager.Instance.New(myItem, 5);
            GameObjectPoolManager.Instance.New(otherItem, 5);
        }
        void OnGridReposition() 
        {
            if ((scrollView.verticalScrollBar != null && scrollView.verticalScrollBar.value >= 0.9) || isScrollViewForceBottom)
            {
                scrollView.ResetPosition();
                isScrollViewForceBottom = false;
            }
        }
        public void Init()
        {
            InitChatList();
            gameObject.SetActive(true);
            GetComponent<PageTweener>().Play();
        }
        void InitChatList() 
        {
            maxNum = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.MaxChatNumber;
        }
        void OnDestroy()
        {
            GameObjectPoolManager.Instance.DestoryObjOfPool(myItem);
            GameObjectPoolManager.Instance.DestoryObjOfPool(otherItem);
        }
        public void Close()
        {
            gameObject.SetActive(false);
        }
        /// <summary>
        /// 发送
        /// </summary>
        public void OnSendClick() 
        {
            content = input.value;
            if (content.Length > 40)
            {
                 BoxManager.CreatePopupTextBox(TableTextBlo.GetText("talkNumsFull"));
                    return;
            }
            else if (content.Length == 0)
            {
                 BoxManager.CreatePopupTextBox(TableTextBlo.GetText("whatDoYouSay"));
                    return;
            }
            else
            {
                SendChat();
            }
        }
        void SendChat() 
        {
           
            CSChatTcp msg = new CSChatTcp();
            msg.chatType = 6;
            msg.content = content;
            NetAPI.SendSocket(OpDefine.CSChatTcp, msg);
            Clean();
        }
        void Update() 
        {
            textNums.text = input.value.Length + "/40";
            if (ChatManager.Instance.newFightChatInfo == true)
            {
                RefreshChatItemList();
                ChatManager.Instance.newFightChatInfo = false; 
            }
        }
        bool isScrollViewForceBottom = false;
        void RefreshChatItemList() 
        {
            isScrollViewForceBottom = items.Count == 0;
            int index = lastShawInfo == null ? -1 : ChatManager.Instance.fightChatInfoQue.IndexOf(lastShawInfo);
            int leftNum = ChatManager.Instance.fightChatInfoQue.Count - index - 1;
            int oldNum = leftNum + items.Count - maxNum;
            for (int i = 0; i < oldNum; ++i)
            {
                RecycleItem(items[i]);
                items.RemoveAt(i);
            }
            for (int i = index + 1; i < ChatManager.Instance.fightChatInfoQue.Count; ++i)
            {
                var info = ChatManager.Instance.fightChatInfoQue[i];
                if (info.playerId == StaticData.playerData.playerId)
                {
                    //自己发送的消息
                    items.Add(CreateMyItem(info));
                }
                else
                {
                    //别人发送的消息
                    items.Add(CreateOtherItem(info));
                }
            }
            if (ChatManager.Instance.fightChatInfoQue.Count == 0)
            {
                lastShawInfo = null;
            }
            else
            {
                lastShawInfo = ChatManager.Instance.fightChatInfoQue[ChatManager.Instance.fightChatInfoQue.Count - 1];
            }
            ResetPosition();
        }

        void ResetPosition() 
        {
            grid.repositionNow = true;
        }
        void RecycleItem(GameObject go) 
        {
            GameObjectPoolManager.Instance.Unspawn(go);
        }
        GameObject CreateMyItem(ChatInfo info) 
        {
            GameObject go = GameObjectPoolManager.Instance.Spawn(myItem);
            go.transform.parent = grid.transform;
            go.transform.localPosition = Vector3.zero;
            go.transform.localScale = Vector3.one;
            go.GetComponent<MyChatItem>().InitMyChatItem(info.content, info.icon, info.toName);
            return go;
        }
        GameObject CreateOtherItem(ChatInfo info) 
        {
            GameObject go = GameObjectPoolManager.Instance.Spawn(otherItem);
            go.transform.parent = grid.transform;
            go.transform.localPosition = Vector3.zero;
            go.transform.localScale = Vector3.one;
            go.GetComponent<OtherChatItem>().InitOtherItem(info.playerName, info.time, info.content, info.icon, info.playerId.ToString(), info.group, info.fightPower, info.level, info.serverId, "UIUnionFightChatPage");
            return go;
        }
        void Clean() 
        {
            textNums.text = "0/40";
            input.value = "";
            input.label.GetComponent<UILabel>().text = "";
            content = "";
        }

    }
}
